#include "Chrone.h"
namespace Chrono{


Date::Date(int yy, Month mm, int dd):y(yy), m(Month(mm)), d(dd) 
{
	if (!is_date(yy, mm, dd)) throw Invalid();
}

Date::Date():y(default_date().year()), 
			 m(default_date().month()), 
			 d(default_date().day())
{}


void Date::add_day(int n)
{
	for (int i = 1; i <=n; ++i)
		if (++d > 31) d = 1;
}

bool is_date(int y, Date::Month m, int d)
{
	if (m<Date::january || Date::december < m) return false;
	
	int days_in_month(31);
	if (d<1) return false;

	switch (m)
	{
		case Date::february:
			days_in_month = (leapyear(y))?29:28;
			break;
		case Date::april: case Date::june: case Date::september: case Date::november:
			days_in_month = 30;
			break;
	}
	if (d > days_in_month) return false;
	return true;
}

bool leapyear(int y)
{
	return false;
}

bool operator==(const Date& d1, const Date& d2)
{
	return (d1.year()==d2.year()&&d1.month()==d2.month()&&d1.day()==d2.day());
}

bool operator!=(const Date& d1, const Date& d2)
{
	return !(d1==d2);
}

std::istream& operator >> (std::istream& ins, Date& dd)
{
	int y, m, d;
	char ch1, ch2, ch3, ch4;
	ins >> y>> ch2 >> m >> ch3 >> y;
	if (!ins) return ins;
	if (ch1!=':'||ch2!=':')
	{
		ins.clear(std::ios_base::failbit);
		return ins;
	}

	dd = Date(y, Date::Month(m), y);
	return ins;
}


std::ostream& operator << (std::ostream& outs, Date d)
{
	outs << d.day() << ':' << d.month() << ':' << d.year();
	return outs;
}

void Date::add_day(int n)
{
}

void Date::add_month(int n)
{
}

void Date::add_year(int n)
{
	if (m==february && d==29 && !leapyear(y+n))
	{
		m = march;
		d = 1;
	}
	y+=n;
}


const Date& default_date()
{
	static const Date dd(2001, Date::january, 1);
	return dd;
}

}